<template>
<div>
  <div style="margin-bottom: 20px">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }" style="margin-left: 20px">社区信息管理</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">文件管理</a></el-breadcrumb-item>
    </el-breadcrumb>
  </div>

  <div style="margin: 10px 0">
    <el-input style="width: 200px" placeholder="请输入文件名称..." suffix-icon="el-icon-search" class="ml-5"
              v-model="name"></el-input>
    <el-button class="ml-5" type="primary" @click="seachKehu">搜索</el-button>
    <el-button @click="resetForm">重置</el-button>
  </div>
  <div style="margin: 10px 0">
    <el-button type="danger" @click="delbath">批量删除<i class="el-icon-remove-outline" style="margin-left: 2px"></i></el-button>
    <el-upload
        action="http://localhost:8888/api/file/upload" style="display: inline-block" :show-file-list="false"
        :accept="xls" :on-success="impOK" :on-error="imperr"
    >
      <el-button type="primary" class="ml-5">上传文件<i class="el-icon-top" style="margin-left: 2px"></i></el-button>
    </el-upload>
  </div>
  <el-table :data="tableData" border stripe :header-cell-class-name="headerBg"
            @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55">
    </el-table-column>
    <el-table-column type="index" label="序号" width="50" fixed></el-table-column>
    <el-table-column prop="name" label="文件名称" width="100px">
    </el-table-column>
    <el-table-column prop="type" label="文件类型" width="100px">
    </el-table-column>
    <el-table-column prop="size" label="文件大小(KB)" width="120px">
    </el-table-column>
    <el-table-column prop="url" label="下载" width="200px">
      <template slot-scope="scope">
      <el-button type="success" round @click="download(scope.row.url)">下载</el-button>
      </template>
    </el-table-column>
    <el-table-column prop="is_enable" label="启用" width="100px">
      <template slot-scope="scope">
        <el-switch
            v-model="scope.row.isEnable"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
            @change="cahangeState(scope.row)">
        </el-switch>
      </template>
    </el-table-column>
    <el-table-column prop="zhaopian" label="预览图" width="160px">
      <template slot-scope="scope">
        <img :src="scope.row.url" min-width="80" height="80"/>
      </template>
    </el-table-column>
    <el-table-column label="操作"  fixed="right" width="200px">
      <template slot-scope="scope">
        <el-button type="primary"  @click="updatekehu(scope.row)"><i class="el-icon-edit"></i>编辑</el-button>
        <el-popconfirm
            class="ml-5"
            confirm-button-text='确定'
            cancel-button-text='取消'
            icon="el-icon-info"
            icon-color="red"
            title="确定要删除这条记录吗？"
            @confirm="deletekehu(scope.row.id)"
        >
          <el-button type="danger" slot="reference"><i class="el-icon-remove-outline"></i>删除</el-button>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <div style="padding: 10px 0">
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10,15,20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
  </div>


  <el-dialog title="修改文件名称" :visible.sync="dialogFormVisible" width="30%">
    <el-form :model="form" label-position="left">
      <el-form-item label="文件名称:" label-width="100px">
        <el-col :span="16">
          <el-input v-model="form.name" autocomplete="off" placeholder="请输入文件名"></el-input>
        </el-col>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="saveKehu">确 定</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "File",
  data(){
    return{
      tableData:[],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      name: "",
      form:{
        id:"",
        name:"",
        type:"",
        size:"",
        url:"",
        isEnable:""
      },
      dialogFormVisible: false,
      imageUrl: '',
      headerBg: 'headerBg',
      multipleSelection: []
    }
  },
  created() {
    this.load()
  },
  methods: {
    cahangeState(data){
      request.post("file/updateEnable",data).then(res => {
        if (res.code == "200") {
          this.load()
          this.$message.success("修改成功")
        } else {
          this.$message.error("修改失败")
        }
      })
    },
    download(data) {
      window.open(data);
    },
    imperr(){
      this.$message.error("文件上传失败")
    },
    impOK(){
      this.$message.success("文件上传成功")
      this.load()
    },
    delbath(){
      let ids=this.multipleSelection.map(v =>v.id)
      request.post("file/deletebatch",ids).then(res => {
        if (res.code == "200") {
          this.load()
          this.$message.success("删除成功")
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    deletekehu(id) {
      console.log(id)
      request.post("file/delete",id).then(res => {
        if (res.code == "200") {
          this.load()
          this.$message.success("删除成功")
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    updatekehu(row){
      this.form=row
      this.dialogFormVisible=true
    },
    saveKehu(){
      this.dialogFormVisible = false
      request.post("file/update",this.form).then(res =>{
        if (res.code=="200"){
          this.load()
          this.$message.success("修改文件名称成功")
        }else {
          this.$message.error("修改文件名称失败")
        }
      })
    },
    resetForm() {
      this.name = ""
      this.load()
    },
    seachKehu() {
      this.load()
    },
    handleCurrentChange(data) {
      this.pageNum = data
      this.load()
    },
    handleSizeChange(data) {
      this.pageSize = data
      this.load()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    load() {
      request.get("file/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name
        }
      }).then(res => {
        if (res.code=="200"){
          this.tableData = res.data.records
          this.total = res.data.total
        }

      })
    }
  }

}
</script>

<style scoped>

</style>